package com.group.sliding.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.sliding.annotation.CacheRemoveByUserId;
import com.group.sliding.constant.Constants;
import com.group.sliding.domain.po.User;
import com.group.sliding.domain.query.UserPageQuery;
import com.group.sliding.enums.UserStatus;
import com.group.sliding.result.PageDTO;
import com.group.sliding.result.Result;
import com.group.sliding.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "管理员用户相关接口")
@RestController
@RequestMapping("/admin/users")
@RequiredArgsConstructor
public class AdminUserController {
    private final IUserService userService;

    @ApiOperation("分页查询用户信息接口")
    @GetMapping("/page")
    public Result<PageDTO<User>> queryUserByPage(UserPageQuery query) {
        log.info("分页查询用户信息 {}", query);
        Page<User> pageResult = userService.search(query);
        PageDTO<User> result = PageDTO.of(pageResult, User.class);
        log.info("分页查询用户信息结果 {}", result);
        return Result.success(result);
    }

    @ApiOperation("更新用户状态")
    @PutMapping("/status/{id}/{status}")
    @CacheRemoveByUserId(keys = Constants.REDIS_KEY_USERS, removeAll = true)
    public Result<String> updateUserStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        log.info("更新用户状态 用户id: {}, status: {}", id, status);
        User user = new User();
        user.setId(id);
        user.setStatus(UserStatus.of(status));
        userService.updateById(user);
        log.info("更新用户状态 用户id: {}, status: {}", id, status);
        return Result.success();
    }
}
